//
//  YAYIMyHospitalViewController.m
//  YAYIDoctor
//
//  Created by Veer on 16/5/13.
//
//

#import "YAYIMyHospitalViewController.h"
#import "YAYIAddHospitalViewController.h"

#import "YAYIMyHospitalCell.h"
#import "YAYIMyHospitalModel.h"


@interface YAYIMyHospitalViewController ()

@property (nonatomic, strong) NSMutableArray *hospitalArray;

@end

@implementation YAYIMyHospitalViewController


- (NSMutableArray *)hospitalArray
{
    if (nil == _hospitalArray) {
        _hospitalArray = [NSMutableArray array];
    }
    return _hospitalArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // Uncomment the following line to preserve selection between presentations.
    self.title = @"我的医院";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
    
    [self setupNav];
    [self getHospitlList];
}


- (void)setupNav
{
    NSString *rightBtnTitleStr = @"添加";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [button setImage:[UIImage imageNamed:@"back_button"] forState:UIControlStateNormal];
    [button setTitle:rightBtnTitleStr forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    
    button.frame = (CGRect){{0, 0}, CGSizeMake(70, 30)};
    
    // 让按钮内部的所有内容左对齐
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 修改导航栏左边的item
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}


#pragma mark - 获取数据
- (void)getHospitlList
{
    [self.hospitalArray removeAllObjects];
    
    [YAYINetWorking getWithURL:me_addHospitalList param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            YAYILog(@"返回：%@", dict);
               
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
                return;
            }
            
            NSArray *dictArray = dict[@"data"];
            for (NSDictionary *dict in dictArray) {
                YAYIMyHospitalModel *model = [[YAYIMyHospitalModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.hospitalArray addObject:model];
            }
            
            [self.tableView reloadData];
        }
        
        // 取消显示hud
        //        [SVProgressHUD dismiss];
    }];
}


#pragma mark - 点击事件
- (void)addAction:(UIButton *)sender
{
    YAYILog(@"点击了添加");
    YAYIAddHospitalViewController *addVc = [[YAYIAddHospitalViewController alloc] init];
    __block typeof(self) weakself = self;
    addVc.refreshOperation = ^(){
        [weakself getHospitlList];
    };
    
    [self.navigationController pushViewController:addVc animated:YES];
}


#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.hospitalArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YAYIMyHospitalModel *model = self.hospitalArray[indexPath.row];
    
    YAYIMyHospitalCell *cell = [YAYIMyHospitalCell cellWith:tableView];
    cell.model = model;
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
